/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.vng.jcore.common.Config;
import db.Redis_W;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.util.ajax.JSONObjectConvertor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.lang.reflect.Type;

/**
 *
 * @author LinhTA
 */
public class UtilConfig {

    private UtilConfig() {
    }

    public static UtilConfig getInstance() {
        return UtilConfigHolder.INSTANCE;
    }

    private static class UtilConfigHolder {

        private static final UtilConfig INSTANCE = new UtilConfig();
    }

    public static String getConfigs() {

        return "";
    }

    private static List<String> listJsonFile() {
//         String path = "../config";

        File currDir = new File(".");
        String path = currDir.getAbsolutePath();
        path = path.substring(0, path.length() - 1);
        path = path + "config";

        List<String> list = new ArrayList<String>();

         //get list file 
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        for (File listOfFile : listOfFiles) {
            if (listOfFile.getName().endsWith(".xfj") || listOfFile.getName().endsWith(".XFJ")) {
                list.add(path + '/' + listOfFile.getName());
            }
        }
        //String content = Files.toString(new File("file.txt"), Charsets.UTF_8);

        return list;
    }

    private static List<Object> getValFiles(List<String> files) {

        List<Object> objs = new ArrayList<Object>();
        Gson jsonalize = new Gson();
        for (int i = 0; i < files.size(); i++) {
            Object object = new Object();

            try {
                File file = new File(files.get(i));
                String content = Files.toString(file, Charsets.UTF_8);

                Object parse = JSONValue.parse(content);

                //JSONObject obj = JSONObject.class.;
                //object = jsonalize.fromJson(content, String.class);
                //objs.add(object);
                objs.add(parse);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        return objs;
    }

    public static void updateConfigDB() {
        List<Object> s = getValFiles(listJsonFile());

        for (int i = 0; i < s.size(); i++) {
            Object object = s.get(i);

            Map<String, Object> result = (HashMap<String, Object>) object;

             //List<String> keys = new ArrayList<String>();
            for (Map.Entry<String, Object> entry : result.entrySet()) {
                String key = entry.getKey();
                Object obj = entry.getValue();

                Map<String, Object> result2 = (HashMap<String, Object>) obj;
                Map<String, String> map = new HashMap<String, String>();
                for (Map.Entry<String, Object> entry1 : result2.entrySet()) {
                    String field = entry1.getKey();
                    Object val = entry1.getValue();

                    map.put(field, val.toString());
                }

//                 Type mapType = new TypeToken<HashMap<String, HashMap>>(){}.getType();  
//                Map<String, String> son = new Gson().fromJson(val.toString(),mapType);//HashMap.class );
                // Map<String, String> reval = new HashMap<String, String>();
                // reval = (HashMap<String, String>)val;
                db.Redis_W.getInstance().del(key);
                db.Redis_W.getInstance().hset(key, map);
                Redis_W.getInstance().sadd("config", key);
                 //keys.add(key);
                //System.out.print(val);
            }

        }

    }
}
